<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Trading extends Admin_Controller {
    public function __construct(){
		parent::__construct();
		$this->load->library('curl');
    }
	public function index($page_no=0){
	
	}
	public function test2(){
		echo mt_rand(1,9)/10;
		echo "<hr />";
		echo sprintf("%.2f",$this->randomFloat(0, 1));
	}
	public function randomFloat($min = 0, $max = 1) {
    return $min + mt_rand() / mt_getrandmax() * ($max - $min);
  }
	public function test(){
		// get account1 sessions
		
		$appid="89951420b05b4a63865da182496babdcb45f00c5af93491cba2638c22c6fb3ef";
		$otp_code="663925";
		$account1="test1@sccx.exchange";
		$account1_password="Welcome111";
		$url = "https://auth.sccx.exchange/api/v1/sessions?email=$account1&password=$account1_password&application_id=$appid&otp_code=$otp_code";
		$account1_sessions=$this->curl->simple_post($url);
		echo $account1_sessions."<hr />";
		$account1_sessions=str_replace("\"","",$account1_sessions);
		// get account2 sessions
		$appid="89951420b05b4a63865da182496babdcb45f00c5af93491cba2638c22c6fb3ef";
		$otp_code="663925";
		$account2="test2@sccx.exchange";
		$account2_password="Welcome222";
		$url = "https://auth.sccx.exchange/api/v1/sessions?email=$account2&password=$account2_password&application_id=$appid&otp_code=$otp_code";
		$account2_sessions=$this->curl->simple_post($url);
		echo $account2_sessions."<hr />";
		$account2_sessions=str_replace("\"","",$account2_sessions);
		//get currencies
/*	$url="https://t.sccx.exchange/api/v2/currencies";
		$headers = array(
			"Token"=>"Bearer ".$account1_sessions,
			);
		$currencies=$this->curl->http_header($headers)->simple_get($url);
		$currencies= json_decode($currencies,false);
		foreach ($currencies as $currency) {
			echo $currency->id;
		}
		echo "<hr />\n";*/
		$headers1=array(
			"Authorization"=>" Bearer $account1_sessions",
		);
		//get accounts
		$url="https://t.sccx.exchange/api/v2/accounts";
		$account1=$this->curl->http_header($headers1)->simple_get($url);
		echo $account1."<hr />";

		$headers2=array(
			"Authorization"=>" Bearer $account2_sessions",
		);
		//get accounts
		$url="https://t.sccx.exchange/api/v2/accounts";
		$account2=$this->curl->http_header($headers2)->simple_get($url);
		echo $account2."<hr />";

		//get tickers
				/*
		$url="https://t.sccx.exchange/api/v2/tickers/".$market;
		$ticker=$this->curl->http_header($headers2)->simple_get($url);
		echo $ticker."<hr />";
		$ticker=json_decode($ticker,false);
		//get the middle price
		$buy=$ticker->ticker->buy;
		$sell=$ticker->ticker->sell;*/
		$market="btcaud";
		$url="https://t.sccx.exchange/api/v2/order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->http_header($headers2)->simple_get($url);
		echo $order_book."<hr />";
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;

		$my_price=$buy+($sell-$buy)*$price_rand;
		echo "买入价$buy,卖出价:$sell,本轮定价:$my_price<hr />";
	/*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account1 sell order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->http_header($headers1)->simple_post($url, $post_data);
		echo "account1卖出订单".$create_sell_order."<hr />";

		//create account2 buy order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->http_header($headers2)->simple_post($url, $post_data);
		echo "account2买入订单".$create_buy_order."<hr />";

		//create account2 sell order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->http_header($headers2)->simple_post($url, $post_data);
		echo "account2买入订单".$create_sell_order."<hr />";

		//create account1 buy order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->http_header($headers1)->simple_post($url, $post_data);
		echo "account1买入订单".$create_buy_order."<hr />";
	}
}